package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.PictureMapper;
import cn.tedu.csmall.product.mapper.SkuMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.VO.AlbumListItemVO;
import cn.tedu.csmall.product.pojo.VO.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.service.IAlbumService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理相册数据的业务实现类
 *
 * @author 田猪益
 * @version 0.0.1
 */
@Slf4j
@Service
public class AlbumServiceImpl implements IAlbumService {
    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private PictureMapper pictureMapper;
    @Autowired
    private SkuMapper skuMapper;
    @Autowired
    private SpuMapper spuMapper;
    @Override
    public void addNew(AlbumAddNewDTO albumAddNewDTO) {
        log.debug("开始处理【添加相册】的业务，参数：{}",albumAddNewDTO);
        String name = albumAddNewDTO.getName();
        int countByName = albumMapper.countByName(name);
        if(countByName>0){
            String message = "添加相册失败，相册名称已被占用";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Album album = new Album();
        BeanUtils.copyProperties(albumAddNewDTO,album);
        log.debug("准备插入相册数据，参数：{}",album);
        int rows = albumMapper.insert(album);
        if(rows!=1){
            String message = "添加相册失败，服务器繁忙，请稍后再试!";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        AlbumStandardVO i = albumMapper.getStandardById(id);
        if(i==null){
            String message = "相册不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int i1 = pictureMapper.countByAlbumId(id);
        if(i1>0){
            String message = "删除相册失败，相册里任有照片";
            throw new ServiceException(ServiceCode.ERR_UNKNOWN,message);
        }
        int i2 = skuMapper.countByAlbum(id);
        if(i2>0){
            String message = "删除相册失败，sku与相册关联";
            throw new ServiceException(ServiceCode.ERR_UNKNOWN,message);
        }
        int i3 = spuMapper.countByAlbum(id);
        if(i3>0){
            String message = "删除相册失败，spu与相册关联";
            throw new ServiceException(ServiceCode.ERR_UNKNOWN,message);
        }
        int rows = albumMapper.deleteById(id);
        if(rows!=1){
            String message = "删除相册失败，服务器繁忙，请稍后再试!";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        log.debug("已删除id={}的相册",id);
        log.debug("受影响行数{}",rows);
    }

    @Override
    public void updateInfoById(Long id, AlbumUpdateDTO albumUpdateDTO) {
        log.debug("开始处理修改相册详情业务，参数{}",albumUpdateDTO);
        //调用mapper的getStandardById()执行查询，检查数据是否存在
        AlbumStandardVO i = albumMapper.getStandardById(id);
        //是：数据不存在，抛出异常
        if(i==null){
            String message = "修改失败，相册不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int i1 = albumMapper.countByNameAndNotId(id, albumUpdateDTO.getUsername());
        if(i1>0){
            String message = "修改失败，相册名称被占用";
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        //创建album对象
        Album album = new Album();
        album.setId(id);
        //将参数对象复制到新创建的album对象
        BeanUtils.copyProperties(albumUpdateDTO,album);
        log.debug("即将执行修改，参数{}",album);
        //调用mapper的update方法执行修改
        int rows = albumMapper.update(album);
        if(rows!=1){
            String message = "修改类别失败，服务器繁忙，请稍后再试!";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
        log.debug("修改完成");
    }

    @Override
    public AlbumStandardVO getStandardById(Long id) {
        log.debug("开始处理查询相册详情业务，参数{}",id);
        //调用Mapper的getStandardById()方法，执行查询
        AlbumStandardVO albumStandardVO = albumMapper.getStandardById(id);
        //判断查询结果是否为null，若是抛出异常
        if(albumStandardVO==null){
            String message = "查询相册详情失败，尝试访问的数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //返回查询结果
        return albumStandardVO;
    }

    @Override
    public List<AlbumListItemVO> list() {
        //调用mapper的list()方法查询列表，获取返回结果
        List<AlbumListItemVO> list = albumMapper.list();
        return list;
    }
}
